package Week4;

import java.util.Scanner;

public class Day24 {
}

//牛客:JZ79 判断是不是平衡二叉树
class Solution3 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param pRoot TreeNode类
     * @return bool布尔型
     */
    public boolean IsBalanced_Solution (TreeNode pRoot) {
        // write code here
        if(pRoot == null) return true;
        int Hleft = geth(pRoot.left);
        int Hright = geth(pRoot.right);
        return  Math.abs(Hleft - Hright) <= 1 && IsBalanced_Solution(pRoot.left) && IsBalanced_Solution(pRoot.right);
    }

    public int geth(TreeNode root){
        if(root == null) return 0;
        int leftH = geth(root.left);
        int rightH = geth(root.right);
        return Math.max(leftH , rightH) + 1;
    }
}

//牛客:DP10 最大子矩阵
class Main14 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        //二维前缀和数组
        int[][] dp = new int[n+1][n+1];
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                int x = in.nextInt();
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + x;
            }
        }
        int ret = Integer.MIN_VALUE;
        //枚举所有的子矩阵
        for(int x1 = 1; x1 <= n; x1++){
            for(int y1 = 1; y1 <= n; y1++){
                for(int x2 = x1; x2 <= n; x2++){
                    for(int y2 = y1; y2 <= n; y2++){
                        //计算所有子矩阵的最大值
                        ret = Math.max(ret , dp[x2][y2] - dp[x1 - 1][y2] - dp[x2][y1 - 1] + dp[x1 - 1][y1 - 1]);
                    }
                }
            }
        }
        System.out.println(ret);
    }
}

//牛客:小葱的01串
class Main15 {
    //滑动窗口
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String s = in.next();
        char[] ch = s.toCharArray();
        //统计0和1的总个数
        int[] sum = new int[2];
        for(int i = 0; i < ch.length; i++){
            sum[ch[i] - '0']++;
        }

        //记录当前区间内的0和1的个数
        int[] count = new int[2];
        int left = 0 , right = 0 , ret = 0 , half = n / 2;//区间长度定为总长的一半
        while(right < n - 1){
            //入窗口
            count[ch[right] - '0']++;
            //出窗口
            while(right - left + 1 > half){
                count[ch[left++]]--;
            }
            if(right - left + 1 == half){
                //如果此时区间0和1的个数正好为总体个数的一半,那么就代表这个区间有两种染色方案
                if(count[0] * 2 == sum[0] && count[1] * 2 == sum[1]){
                    ret += 2;
                }
            }
        }
        System.out.println(ret);
    }
}